import { objectType } from "nexus";
import { UserSecurity } from "nexus-prisma";

function desensitizationPhoneNumber(value?: string): string | undefined {
    if (value && typeof value === "string") {
        return value.replace(/(.*)\d{4}(\d{4})/, '$1****$2');
    }

    return value;
} 

// Define a user security entity.
export const UserSecurityEntity = objectType({
    name: UserSecurity.$name,
    description: UserSecurity.$description,
    definition(t) {
        t.field(UserSecurity.userId);
        t.field(UserSecurity.user);
        t.nonNull.field(UserSecurity.phoneVerified);
        t.field({
            ...UserSecurity.phone,
            resolve: (source) => desensitizationPhoneNumber(source.phone),
        });
        t.nonNull.boolean("hasTotp", {
            resolve: (source) => !!source.totpSceret,
        });
        t.nonNull.boolean("hasPassword", {
            resolve: source => !!source.password,
        });
    }
});